/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package onpu_dms_xls.kernel;

import java.io.*;
import java.nio.channels.FileChannel;
import java.sql.Connection;
import onpu_dms_xls.exceptions.MyException;
import onpu_dms_xls.exceptions.MyXLSException;
import onpu_dms_xls.kernel.common.Util;
import onpu_dms_xls.kernel.common.logAndDebug.LoggingFacade;
import onpu_dms_xls.kernel.common.pdf.composite.ExtractComponent;
import onpu_dms_xls.kernel.common.pdf.composite.new_PDF_ExtractCreator;
import org.apache.log4j.Logger;

/**
 *
 * @author kovbasiuk
 */
public class File_From_DB_PDF extends File_From_DB {
    

    public File_From_DB_PDF(String inputXML, Connection con) throws MyException {
        super(inputXML, con);
    }

    @Override
    protected void doFileTypeRelatedProcessing() throws MyException {
        LoggingFacade.trace.info("call");
        try {
            FileChannel ic = new FileInputStream(templateXLS.getAbsolutePath()).getChannel();
            FileChannel oc = new FileOutputStream(input.getConstantParam_outputFolder() + '/' + TEMPORARY_XLS_FILE_NAME + ".xml").getChannel();
            ic.transferTo(0, ic.size(), oc);
            ic.close();
            oc.close();
        } catch (IOException e) {
            throw new MyXLSException(
                    "XLS_From_DB",
                    "make_XLS_File_From_DB/ 3.1 section",
                    "error while copying+renaming template file",
                    e.getMessage());
        }

        res = new File(input.getConstantParam_outputFolder() + '/' + TEMPORARY_XLS_FILE_NAME + ".xml");
        LoggingFacade.trace.debug("resulting PDF data alingments xml file location:" + res.getAbsolutePath());

        String resultPDF_FileName = input.getConstantParam_outputFileName() + ".pdf";
        LoggingFacade.trace.debug("will create pdf result file: " + resultPDF_FileName);

        File realPDFresult = null;


//                PDF_Extract_LayoutTemplate pdf_extract_AlignmentTemplate = Util.getPDFVipiskaTemplateFromPDFXMLTemplate(res);
//                LoggingFacade.trace.info("successfully read pdf_extract_AlignmentTemplate from xml");
//
//                PDF_Extract_Creator pdfMaker = new PDF_Extract_Creator(
//                        input.getConstantParam_outputFolder() + '/' + resultPDF_FileName,
//                        pdf_extract_AlignmentTemplate,
//                        varDeclArray,
//                        input,
//                        con);
//                pdfMaker.loadDataFromDBToVipiska_Data();
//
//                try{
//                    realPDFresult = pdfMaker.makeFile();
//                } catch (Exception e){
//                    LoggingFacade.trace.info("exception in pdfMaker.makeFile();"+e.getMessage());
//                    return null;
//                }

        ExtractComponent pdf_template;
        LoggingFacade.trace.debug("reading pdf_extract_AlignmentTemplate from xml");
        try {
            pdf_template = Util.getNewPDFExtractTemplateFromPDFXMLTemplate(res);
        } catch (IOException e) {
            throw new MyException("", e);
        }
        LoggingFacade.trace.info("successfully read pdf_extract_AlignmentTemplate from xml");

        pdf_template.print();

        new_PDF_ExtractCreator new_pdfMaker = new new_PDF_ExtractCreator(
                input.getConstantParam_outputFolder() + '/' + resultPDF_FileName,
                pdf_template,
                varDeclArray,
                input,
                conn);
        new_pdfMaker.loadDataFromDBToVipiska_Data();
        try {
            realPDFresult = new_pdfMaker.makeFile();
        } catch (Exception e) {
            e.printStackTrace(System.out);
            LoggingFacade.trace.error("exception in pdfMaker.makeFile();",e);
            res = null;
            return;
        }

        try {
            res.delete();
        } catch (Exception e) {
        }
        res = realPDFresult;
    }
}
